package com.whlilong.jxc.app;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


import org.eclipse.core.runtime.IPlatformRunnable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.model.Regist;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.whlilong.jxc.sys.Login;
import com.whlilong.jxc.sys.Register;
import com.whlilong.jxc.sys.SaveLogInfo;
import com.whlilong.jxc.util.FileUtil;

/**
 * This class controls all aspects of the application's execution
 */
@SuppressWarnings("deprecation")
public class Application implements IApplication {

	private String month;
	private String day;

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.
	 * IApplicationContext)
	 */
	public Object start(IApplicationContext context) {

		Display display = PlatformUI.createDisplay();
		try {
			Platform.endSplash();
			// 文件锁实例。。。
			InstanceControl ic = new InstanceControl();
			if (!ic.isRunning())
				MessageDialog.openInformation(null, "提示", "武汉立隆商务信息管理系统已启动！");
			else {
				Calendar c = Calendar.getInstance();
				int curYear = Calendar.getInstance().get(c.YEAR)* 27;
				int curMonth = (Calendar.getInstance().get(c.MONTH)+1) * 12;
				int curDay = Calendar.getInstance().get(c.DAY_OF_MONTH)*15;
				String pass = curYear + "-"+ "O"+"-"+curMonth+"-"+"V"+"-"+curDay+"-"+"ER";
				List<Regist> list = ExtentionInfo.getInstance().getRegist();
				if (list.size() <= 0 || list.get(0).getStatus().equals("0")) {
					if (list.size() <= 0) {
						Regist rg = new Regist();
						rg.setRegistcode("");
						rg.setPass(pass);
						rg.setStatus("0"); // 0--未注册 1--已注册
						DbManager.getInstance().insert(rg);
						// FileUtil.writeTxt(FileUtil.getDogPath(), code);
						list = ExtentionInfo.getInstance().getRegist();
					}
					if (list.get(0).getStatus().equals("0")) {
						Register regist = new Register();
						if (!regist.isRegist()) {
							DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
							Date curDate = new Date();
							String pas = list.get(0).getPass();
							String[] str = pas.split("-");
							int str1 = Integer.parseInt(str[0])/27;
							int str2 = Integer.parseInt(str[2])/12;
							if (str2 > 9)
								month = str2 + "";
							if (str2 < 10 && str2 >= 0) {
								month = "0" + str2;
							}
							int str3 = Integer.parseInt(str[4])/15;
							if (str3 > 9)
								day = str3 + "";
							if (str3 < 10 && str3 >= 0) {
								day = "0" + str3;
							}
							pas = str1+"-"+month+"-"+day;
							Date passDate = null;
							try {
								 passDate = df.parse(pas);
								 c.setTime(passDate);
								 c.add(c.DATE, 30);
								 passDate = c.getTime();
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

							if (passDate.before(curDate)) {
								MessageDialog.openWarning(null, "软件过期",
										"软件过期，请与供应商联系注册！");

							} else {
								Login login = new Login();
								if (!login.isLogin()) {
									return IPlatformRunnable.EXIT_OK;
								}
								String tag = SaveLogInfo.getInstance()
										.getUsertag();
								if (tag.equals("2")) {
									int returnCode = PlatformUI
											.createAndRunWorkbench(
													display,
													new ClientWorkbenchAdvisor());
									if (returnCode == PlatformUI.RETURN_RESTART) {
										return IApplication.EXIT_RESTART;
									}
								} else {

									int returnCode = PlatformUI
											.createAndRunWorkbench(
													display,
													new ApplicationWorkbenchAdvisor());

									if (returnCode == PlatformUI.RETURN_RESTART) {
										return IApplication.EXIT_RESTART;
									}
								}
							}
						} else {
							list = ExtentionInfo.getInstance().getRegist();
							Regist rg = list.get(0);
							// rg.setDate(list.get(0).getDate());
							// rg.setRegistcode(list.get(0).getRegistcode());
							rg.setStatus("1"); // 0--未注册 1--已注册
							DbManager.getInstance().update(rg);
							list = ExtentionInfo.getInstance().getRegist();
						}
					}
				}
				
				if (list.get(0).getStatus().equals("1")) {
					
					String registcode = FileUtil.readRegistcode(list.get(0).getRegistcode());
					if (list.get(0).getRegistcode().equals(registcode.trim())) {
						Login login = new Login();
						if (!login.isLogin()) {
							return IPlatformRunnable.EXIT_OK;
						}
						String tag = SaveLogInfo.getInstance().getUsertag();
						if (tag.equals("2")) {
							int returnCode = PlatformUI.createAndRunWorkbench(
									display, new ClientWorkbenchAdvisor());
							if (returnCode == PlatformUI.RETURN_RESTART) {
								return IApplication.EXIT_RESTART;
							}
						} else {

							int returnCode = PlatformUI.createAndRunWorkbench(
									display, new ApplicationWorkbenchAdvisor());

							if (returnCode == PlatformUI.RETURN_RESTART) {
								return IApplication.EXIT_RESTART;
							}
						}
					} else {
						MessageDialog.openWarning(null, "加密狗加载失败", "加密狗未插入或加密狗号不匹配！");
					}
				}
			}
			return IApplication.EXIT_OK;
		} finally {
			display.dispose();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.equinox.app.IApplication#stop()
	 */
	public void stop() {
		final IWorkbench workbench = PlatformUI.getWorkbench();
		if (workbench == null)
			return;
		final Display display = workbench.getDisplay();
		display.syncExec(new Runnable() {
			public void run() {
				if (!display.isDisposed())
					workbench.close();
			}
		});
	}
}
